package com.sl.au.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.service.AccountService;
import com.sl.au.service.MobileMessageSend;
import com.sl.au.service.ModuleService;
import com.sl.au.service.OperateLogService;
import com.sl.au.service.UserService;
import com.sl.utils.CRM;

@RestController
@RequestMapping(value = "/ui/login")
public class LoginController extends BaseController {

	@Autowired
	AccountService accountService;
	@Autowired
	UserService userService;
	@Autowired
	ModuleService moduleService;

	@Autowired
	OperateLogService operateLogService;
	
	@Autowired
	MobileMessageSend mobileMessageSend;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView) {
		modelAndView.setViewName("login/login");
		return modelAndView;
	}

	@RequestMapping(value = "submit", method = RequestMethod.POST)
	public Map<String, Object> login(HttpServletRequest request, @RequestBody Account account) {

		//request.getSession().invalidate();
		boolean isCodeAcc = true;//验证码是否通过
		String smsCode = account.getSmsCode();
		Boolean isMobile=this.isMobile();
		Map<String, Object> map = new HashMap<String, Object>();
		int code = 0;
		account.setId(null);

		// PC同一会话不同用户处理
		Account oldA = this.getMyAccount();
		if (null != oldA && isMobile == false && !account.getAccountName().equals(oldA.getAccountName())) {
			map.put("code", 3);
			map.put("message", "有其他用户已经在本机登录，<br>避免错误，请关闭所有浏览器再登录！");
			return map;
		}

		Account a = accountService.login(account);
		if (a != null) {
			//验证验证码
			if(smsCode != null && smsCode != ""){//需要验证码
				try {
					String returnCode = mobileMessageSend.verifycode(a.getUser().getContactType(),smsCode);
					if(!returnCode.equals("成功")){//验证码失败
						code = 99;
						isCodeAcc = false;
						map.put("message", returnCode);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(isCodeAcc){
				if (isMobile == false) {
					HttpSession session = request.getSession();
					session.setAttribute("account", a);
				}

				if(isMobile){
					map.put("modules", moduleService.getSimpleModulesByRole(a.getRole().getId()));
					map.put("user", a.getUser());
					map.put("accountId", a.getId());
				}
				code = 1;
				map.put("userId", a.getUser().getId());
				operateLogService.log(a.getUser(), "登录系统", "登录成功");
			}
		}
		map.put("code", code);
		return map;
	}

}
